package com.train.classeval.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.train.classeval.entity.Course;
import com.train.classeval.mapper.CourseMapper;
import com.train.classeval.service.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @description:
 * @program: classeval
 * @author: ChenQi
 * @create: 2021-10-2021/10/22-11-33
 */
@Service
@Transactional
public class CourseServiceImpl implements ICourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    @Transactional
    public int addCourse(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        final int i = courseMapper.addCourse(course);
        return i;
    }

    @Override
    public List<Course> findAllCourse() {
        final List<Course> courses = courseMapper.selectList(null);
        return courses;
    }

    @Override
    public Course findOne(String name) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_name",name);
        final Course course = courseMapper.selectOne(queryWrapper);
        return course;
    }

    @Override
    public Integer deleteOne(Integer cid) {
        int i = courseMapper.deleteById(cid);
        return i;
    }
}
